/*
 * Class: AbstractWebservice
 * 
 * Created on Sep 16, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran;

import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.minh.tran.model.Gold;
import com.minh.tran.service.GetGoldPriceSoapProxy;
import com.minh.tran.util.ItemUtil;

/**
 * The Class AbstractWebservice.
 */
public abstract class AbstractWebservice extends Thread implements IWebserviceComm
{

    /** The Constant LOG. */
    private static final Log LOG = LogFactory.getLog(AbstractWebservice.class);

    /** The running. */
    private boolean running = false;

    /** The sleeping time. */
    private final long sleepingTime = 1000;

    /** The queue. */
    protected final AbstractQueue<Gold> queue = new LinkedBlockingQueue<Gold>();

    /** The list golds. */
    protected final List<Gold> lstGolds = new ArrayList<Gold>();

    /** The proxy. */
    private final GetGoldPriceSoapProxy proxy = new GetGoldPriceSoapProxy();

    /**
     * Instantiates a new abstract web service.
     */
    public AbstractWebservice()
    {
        this.running = true;
    }

    /**
     * Authenticate.
     *
     * @param userName the user name
     * @param password the password
     * @return true, if successful
     */
    protected Gold getCurrentGoldPrice(final String userName, final String password)
    {
        final Gold gold = new Gold(ItemUtil.GOLD);
        try
        {
            final String[] values = this.proxy.getCurrentGoldPrice(userName, password);
            if (values.length > 0)
            {
                gold.setPrice(values[0]);
                gold.setDateTime(values[1]);
            }
        }
        catch (Exception e)
        {
            return null;
        }
        return gold;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Thread#run()
     */
    @Override
    public void run()
    {
        while (this.running)
        {
            try
            {
                this.getGoldPrice();
                AbstractWebservice.sleep(this.sleepingTime);
            }
            catch (InterruptedException e)
            {
                this.running = false;
            }
        }
        super.run();
    }

    /**
     * {@inheritDoc}
     * 
     * @see com.minh.tran.IWebserviceComm#getGoldPrice()
     */
    @Override
    public Gold getGoldPrice()
    {
        final Gold gold = this.getCurrentGoldPrice(Configuration.USER_NAME, Configuration.PASSWORD);
        if (gold != null)
        {
            if (this.isANewGold(gold))
            {
                this.queue.add(gold);
            }
            this.request();
        }
        return null;
    }

    /**
     * Checks if is a new gold.
     *
     * @param gold the gold
     * @return true, if is a new gold
     */
    private boolean isANewGold(final Gold gold)
    {
        return true;
    }

    /**
     * Request.
     */
    protected abstract void request();

    /**
     * {@inheritDoc}
     * 
     * @see com.minh.tran.IWebserviceComm#startProcessing()
     */
    @Override
    public void startProcessing()
    {
        LOG.info("Start processing!");
        this.start();
    }

    /**
     * {@inheritDoc}
     * 
     * @see com.minh.tran.IWebserviceComm#stopProcessing()
     */
    @Override
    public void stopProcessing()
    {
        LOG.info("Stop processing!");
        this.running = false;
    }
}
